package cn.iocoder.yudao.module.exam.service.paper;

import cn.iocoder.yudao.module.exam.controller.admin.question.vo.QuestionRespVO;
import cn.iocoder.yudao.module.exam.dal.dataobject.paperquestion.PaperQuestionDO;
import cn.iocoder.yudao.module.exam.service.paperquestion.PaperQuestionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.exam.controller.admin.paper.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.paper.PaperDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.paper.PaperMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.exam.enums.ErrorCodeConstants.*;

/**
 * 试卷 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PaperServiceImpl implements PaperService {

    @Resource
    private PaperMapper paperMapper;
    @Resource
    private PaperQuestionService paperQuestionService;

    @Override
    public Long createPaper(PaperSaveReqVO createReqVO) {
        // 插入
        PaperDO paper = BeanUtils.toBean(createReqVO, PaperDO.class);
        paperMapper.insert(paper);
        List<QuestionRespVO> selectQuestionList = createReqVO.getSelectQuestionList();
        if(!CollectionUtils.isEmpty(selectQuestionList)&&createReqVO.getPaperType()==0){
            List<PaperQuestionDO> paperQuestionDOList = selectQuestionList.stream().map(qvo -> {
                PaperQuestionDO paperQuestionDO = BeanUtils.toBean(qvo, PaperQuestionDO.class);
                paperQuestionDO.setPaperId(paper.getId());
                return paperQuestionDO;
            }).collect(Collectors.toList());
            paperQuestionService.saveQuestions(paper.getId(),paperQuestionDOList);
        }
        // 返回
        return paper.getId();
    }

    @Override
    public void updatePaper(PaperSaveReqVO updateReqVO) {
        // 校验存在
        validatePaperExists(updateReqVO.getId());
        // 更新
        PaperDO updateObj = BeanUtils.toBean(updateReqVO, PaperDO.class);
        paperMapper.updateById(updateObj);
        List<QuestionRespVO> selectQuestionList = updateReqVO.getSelectQuestionList();
        if(!CollectionUtils.isEmpty(selectQuestionList)&&updateReqVO.getPaperType()==0){
            List<PaperQuestionDO> paperQuestionDOList = selectQuestionList.stream().map(qvo -> {
                PaperQuestionDO paperQuestionDO = BeanUtils.toBean(qvo, PaperQuestionDO.class);
                paperQuestionDO.setPaperId(updateReqVO.getId());
                return paperQuestionDO;
            }).collect(Collectors.toList());
            paperQuestionService.saveQuestions(updateReqVO.getId(),paperQuestionDOList);
        }
    }

    @Override
    public void deletePaper(Long id) {
        // 校验存在
        validatePaperExists(id);
        // 删除
        paperMapper.deleteById(id);
    }

    private void validatePaperExists(Long id) {
        if (paperMapper.selectById(id) == null) {
            throw exception(PAPER_NOT_EXISTS);
        }
    }

    @Override
    public PaperRespVO getPaper(Long id) {
        PaperDO paper = paperMapper.selectById(id);
        PaperRespVO respVO = BeanUtils.toBean(paper, PaperRespVO.class);
        if(respVO.getPaperType()==0){
            List<PaperQuestionDO> questionsByPaperId = paperQuestionService.findQuestionsByPaperId(id);
            List<QuestionRespVO> questionRespVOS = BeanUtils.toBean(questionsByPaperId, QuestionRespVO.class);
            respVO.setSelectQuestionList(questionRespVOS);
        }
        return respVO;
    }

    @Override
    public PageResult<PaperDO> getPaperPage(PaperPageReqVO pageReqVO) {
        return paperMapper.selectPage(pageReqVO);
    }

}